﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace iMusik.Mp3ZingOnline
{
    public class ZingTop100Object
    {
        private string _LocalLink;
        private bool _IsSetInfo;

        private List<Mp3Song> _ListSongs;
        public List<Mp3Song> ListSongs
        {
            get { return _ListSongs; }
            private set { _ListSongs = value; }
        }        

        public ZingTop100Object(string url)
        {
            _LocalLink = url;
            _ListSongs = new List<Mp3Song>();
            _IsSetInfo = false;
        }

        public async Task SetAllInformation()
        {
            if (_IsSetInfo)
                return;
            try
            {
                string source = await Mp3Zing.DownloadString(new Uri(_LocalLink));
                int i = 0;
                while (i < source.Length)
                {
                    if (source[i] == 34 || source[i] == '\n')
                    {
                        source = source.Remove(i--, 1);
                    }
                    i++;
                }

                string pattern = @"(?<=div class=content).*?(</div>)";
                Regex regex = new Regex(pattern);
                MatchCollection mc = regex.Matches(source);
                source = mc[0].Value;

                if (_ListSongs.Count == 0)
                    SetListSong(source);
                _IsSetInfo = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private void SetListSong(string source)
        {
            int i = 0;
            while (i < source.Length)
            {
                if (source[i] == 34)
                {
                    source = source.Remove(i, 1);
                    i--;
                }
                i++;
            }

            List<string> listSongName = GetListName(source);
            List<string> listSingerName = GetListSingerName(source);
            List<string> listSongLocalLink = GetListLocaLink(source);

            i = 0;
            foreach (string link in listSongLocalLink)
            {
                _ListSongs.Add(new Mp3Song(listSongName[i], listSingerName[i], link, true));
                i++;
            }
        }

        private List<string> GetListLocaLink(string source)
        {
            List<string> listSongLocalLink = new List<string>();
            string pattern = @"(?<=href=/bai-hat).*?(html)";
            Regex regex1 = new Regex(pattern);
            MatchCollection mc1 = regex1.Matches(source);
            int j = 0;
            foreach (Match m1 in mc1)
            {
                string temp = "http://m.mp3.zing.vn/bai-hat";
                temp += m1.Value;
                listSongLocalLink.Add(temp);
            }
            return listSongLocalLink;
        }
  
        private List<string> GetListSingerName(string source)
        {
            List<string> listSingerName = new List<string>();
            string pattern = @"(?<=<h4>).*?(?=<)";
            Regex regex3 = new Regex(pattern);
            MatchCollection mc3 = regex3.Matches(source);
            foreach (Match m1 in mc3)
            {
                string temp2 = m1.Value;
                temp2 = Mp3Zing.ConvertStringAsciiToUtf8(temp2);
                listSingerName.Add(temp2);

            }
            return listSingerName;
        }

        private List<string> GetListName(string source)
        {
            List<string> listSongAlbumName = new List<string>();
            string pattern = @"(?<=<h3>).*?(?=<)";
            Regex regex2 = new Regex(pattern);
            MatchCollection mc2 = regex2.Matches(source);
            foreach (Match m1 in mc2)
            {
                string temp2 = m1.Value;
                temp2 = Mp3Zing.ConvertStringAsciiToUtf8(temp2);
                listSongAlbumName.Add(temp2);

            }
            return listSongAlbumName;
        }
    }
}
